package com.springcloud.tread.job;

import cn.hutool.core.date.DateUtil;
import com.springcloud.tread.pojo.Index;
import com.springcloud.tread.pojo.IndexData;
import com.springcloud.tread.service.IndexDataService;
import com.springcloud.tread.service.StoreSerice;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;

////开启定时器，目的：随时刷新第三方数据到缓存中去
//public class IndexDataSyncJob extends QuartzJobBean {
//    @Autowired
//    IndexDataService indexDataService;
//    @Autowired
//    StoreSerice storeSerice;
//
//    @Override
//    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//        System.out.println("定时启动：" + DateUtil.now());
//        List<Index> indexes = storeSerice.fresh(); //获取指数代码
//        for (Index index : indexes) {
//            indexDataService.fresh(index.getCode());//获取指数数据
//        }
//        System.out.println("定时结束：" + DateUtil.now());
//
//    }
//}

@Component
public class IndexDataSyncJob{

    @Autowired
    StoreSerice storeSerice;

    @Autowired
    private IndexDataService indexDataService;

    @Scheduled(cron = "0 */1 * * * ?")
    protected void executeInternal() {
        System.out.println("定时启动：" + DateUtil.now());
        List<Index> indexes = storeSerice.fresh();
        for (Index index : indexes) {
            indexDataService.fresh(index.getCode());
        }
        System.out.println("定时结束：" + DateUtil.now());
    }
}